package com.shark.security.jwt.persistent.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.shark.security.jwt.persistent.entity.UserRoleEntity;

@Repository
public class UserRoleRepository {

	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;
	
	private static final String SQL_GET_BY_USER_ID = "select user_id, role_id from t_sys_user_role where user_id = :userId";
	
	/**
	 * 根据用户登录名的用户数据
	 * @param LoginName
	 * @return
	 */
	public List<UserRoleEntity> getByUserId(Integer userId) {
		Map<String, Object> mapParam = new HashMap<>();
		mapParam.put("userId", userId);
		
		return jdbcTemplate.query(SQL_GET_BY_USER_ID, mapParam, new RowMapper<UserRoleEntity>() {

			@Override
			public UserRoleEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
				UserRoleEntity u = new UserRoleEntity();
				u.setRoleId(rs.getInt("role_id"));
				u.setUserId(rs.getInt("user_id"));
				return u;
			}
			
		});
	}
}
